package com.longrise.umeng.share;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

import java.io.File;

/**
 * Created by Administrator on 2017/12/20.
 * 用于海报分享
 */

public class PosterShare {

    private static final String TAG = "PosterShare";

    private Context context;

    private ShareCallback mCallback;

    public PosterShare(Context context) {
        super();
        this.context = context;
    }

    /**
     * 分享
     *
     * @param flag 区分不同分享平台 1、QQ空间，2、微信好友，3、微信朋友圈，4、QQ好友
     */
    public void share(int flag, String savePath, final String id) {

        UMImage imageLocal;
        ShareAction shareAction = new ShareAction((Activity) context);
        if (flag == 1) {
            shareAction.setPlatform(SHARE_MEDIA.QZONE);  //qq空间
        } else if (flag == 2) {
            shareAction.setPlatform(SHARE_MEDIA.WEIXIN);  //微信
        } else if (flag == 3) {
            shareAction.setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE);  //朋友圈
        } else {
            shareAction.setPlatform(SHARE_MEDIA.QQ);  //QQ
        }
        //把文件地址转化成文件,不支持直接设置文件路径
        File file = new File(savePath);
        imageLocal = new UMImage(context, file);//设置分享图片的文件
        imageLocal.setThumb(new UMImage(context, file));
        shareAction.withMedia(imageLocal);  //分享图片
        shareAction.setCallback(new UMShareListener() {
            /**
             * @descrption 分享开始的回调
             * @param platform 平台类型
             */
            @Override
            public void onStart(SHARE_MEDIA platform) {

            }

            /**
             * @descrption 分享成功的回调
             * @param platform 平台类型
             */
            @Override
            public void onResult(SHARE_MEDIA platform) {
                Log.e(TAG,"------onResult------");
                if (mCallback != null) {
                    mCallback.shareSuccess();
                }
            }
            /**
             * @descrption 分享失败的回调
             * @param platform 平台类型
             * @param t 错误原因
             */
            @Override
            public void onError(SHARE_MEDIA platform, Throwable t) {
                Log.e(TAG,"------onError------");
                if (mCallback != null) {
                    mCallback.shareFailed();
                }
            }

            /**
             * @descrption 分享取消的回调
             * @param platform 平台类型
             */
            @Override
            public void onCancel(SHARE_MEDIA platform) {
                Log.e(TAG,"------onCancel------");
                if (mCallback != null) {
                    mCallback.shareCancel();
                }
            }
        });
        shareAction.share();
    }

    public void setDialogCallback(ShareCallback callback) {
        this.mCallback = callback;
    }


    public interface ShareCallback {

        void shareSuccess();

        void shareFailed();

        void shareCancel();
    }

}
